import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)

const [keyUser,keyCar,keyRoutes] = ['user','car','routes']

export default new Vuex.Store({
  state: {
    user: null,
		car:null,
		busRoutes:null,
  },
  getters: {
    getStorage: function(state) {
      if(state.user == null) {
        state.user = JSON.parse(localStorage.getItem(keyUser))
      }
      return state.user
    },
		getCar:function(state){
			if(state.car == null) {
			  state.car = JSON.parse(sessionStorage.getItem(keyCar))
			}
			return state.car
		},
		getBusRoutes:function(state){
			if(state.BusRoutes == null) {
			  state.BusRoutes = JSON.parse(sessionStorage.getItem(keyRoutes))
			}
			return state.BusRoutes
		}
  },
  mutations: {
    setStorage: function(state, value) {
      state.user = value
      localStorage.setItem(keyUser, JSON.stringify(value))
    },
    removeStorage: function(state) {
      state.user = null
			state.car = null
      localStorage.removeItem(keyUser)
			sessionStorage.removeItem(keyCar)
			sessionStorage.removeItem(keyRoutes)
    },
		setUserCar: function(state,value){
			state.car = value;
			sessionStorage.setItem(keyCar, JSON.stringify(value))
		},
		setBusRoutes: function(state,value){
			state.busRoutes = value;
			sessionStorage.setItem(keyRoutes, JSON.stringify(value))
		}
  }

})